package com.umeox.watch.moto.dataservice.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.umeox.moto.common.log.Logger;
import com.umeox.moto.common.utils.NetUtils;
import com.umeox.moto.common.utils.StringUtils;
import com.umeox.watch.moto.dataservice.base.LauncherManager;


public class LaunchedReceiver extends BroadcastReceiver {

    private final String TAG = LaunchedReceiver.class.getSimpleName();
    private static long lastReceiverTime = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (StringUtils.isEmpty(action)) {
            return;
        }
        Logger.w(TAG, "LaunchedReceiver action :" + action);

        //接受该广播最小间隔要控制下，防止太频繁访问api接口
        long minInterval = 6000;
        if (lastReceiverTime > 0 && System.currentTimeMillis() - lastReceiverTime < minInterval) {
            Logger.w(((System.currentTimeMillis() - lastReceiverTime) / 1000) + "秒前已经收到过该广播了");
            return;
        }
        lastReceiverTime = System.currentTimeMillis();
        if (NetUtils.isNetworkAvailable()) {
            LauncherManager.getInstance().onNetworkAvailable();
        }
    }
}